package com.jh3502.vivo.function.reimbursepages.start;

import android.os.Bundle;

import com.google.gson.Gson;
import com.jh3502.vivo.R;
import com.jh3502.vivo.base.BaseFragment;
import com.jh3502.vivo.function.reimbursepages.ReimListPresenter;
import com.jh3502.vivo.function.reimnew.ReimNewActivity;
import com.jh3502.vivo.function.todo.TodoListBean;
import com.jh3502.vivo.function.tododetail.reim.TodoReimActivity;
import com.jh3502.vivo.utils.Constant;
import com.jh3502.vivo.utils.Utils;
import com.jh3502.vivo.view.MyRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * @author harisucici
 * @version 2018/11/28 14:15
 * name
 * todo
 * @email harisucici@gmail.com
 */
public class ReimStartFragment extends BaseFragment<ReimListPresenter.ReimListView, ReimListPresenter> implements ReimListPresenter.ReimListView {

    List<TodoListBean> dataList = new ArrayList<>(2);
    @BindView(R.id.reim_recycler)
    MyRecyclerView reimRecycler;
    private ReimStartAdapter mAdapter;
    private int page = 0;
    private int p;

    @Override
    public ReimListPresenter initPresenter() {
        return new ReimListPresenter();
    }

    @Override
    protected int setViewId() {
        return R.layout.fragment_reim;
    }

    @Override
    protected void initView() {
        mAdapter = new ReimStartAdapter(mContext, dataList);
        reimRecycler.setAdapter(mAdapter);
        mPresenter.setReimListView(this);

        mAdapter.setOnItemClickListener(s -> {
            Bundle bundle = new Bundle();
            bundle.putInt("source", Constant.REIM_START);
            bundle.putInt("id", dataList.get(s).getId());
            bundle.putString("bean", new Gson().toJson(dataList.get(s)));
            bundle.putString("businessNo", dataList.get(s).getBusinessNo());
            Utils.getInstance().startActivity(mActivity, TodoReimActivity.class, bundle);
        });

        mAdapter.setButtonSetOnclickChange(position -> {
            Bundle bundle = new Bundle();
            bundle.putInt("id", dataList.get(position).getId());
            Utils.getInstance().startActivity(mActivity, ReimNewActivity.class, bundle);

        });

        mAdapter.setButtonSetOnclickDelete(position -> {
            p = position;
            Map<String, Object> body = new HashMap<>(2);
            body.put("id", dataList.get(position).getId());
            mPresenter.toDeleteTask(body);
        });
    }

    @Override
    protected void initData() {


    }

    @Override
    protected void doOnDestroy() {

    }

    @Override
    protected void doOnResume() {
        Map<String, Object> body = new HashMap<>(2);
        body.put("page", page);
        body.put("type", Constant.TYPE_REIM);
        mPresenter.toGetMyTask(body);
    }

    @Override
    protected void doOnVisible() {

    }

    @Override
    protected void doOnInvisible() {

    }

    @Override
    protected void doOnSaveInstanceState(Bundle savedInstanceState) {
    }

    @Override
    public void responseError(int code, String msg) {
        showShortToast(msg);
    }

    @Override
    public void requestFailure(Throwable t) {

    }


    @Override
    public void getMyTasklSuccess(List<TodoListBean> datas) {
        dataList.clear();
        dataList.addAll(datas);
        mAdapter.notifyDataSetChanged();

    }

    @Override
    public void findAuditTaskSuccess(List<TodoListBean> dataList) {

    }

    @Override
    public void agreeForAllSuccess() {

    }

    @Override
    public void deleteAuditSuccess() {

    }

    @Override
    public void deleteTaskSuccess() {
        showShortToast("删除成功");
        dataList.remove(p);
        mAdapter.notifyDataSetChanged();
    }


}
